var mosca = require('mosca')

var settings = {
  port: 1883,
  backend:{
  	type: 'mongo',
  	url: 'mongodb://localhost:27017/mqtt',
  	pubsubCollection: 'ascoltatori',
  	mongo: {}
  },

/**
 * 数据持久化参数设置
 */
  persistence:{
  	factory: mosca.persistence.Mongo,
    url: "mongodb://localhost:27017/mosca"
  }
};

var server = new mosca.Server(settings);


/**
 * 处理客户端连接clientConnected事件处理函数
 */
server.on('clientConnected',function(client){
    console.log('Client Connected:', client.id);
})


/**
 * 处理客户端发布消息published事件处理函数
 */
server.on('published',function(packet,client){
    console.log('Published：', packet.topic);
    //console.log('Client：', client);
})


/**
 * 处理客户端断开连接clientDisconnected事件处理函数
 */
server.on('clientDisconnected', function(client) {
    console.log('Client Disconnected:', client.id);
});


server.on('ready',function(){
    console.log('Mosca server is up and running');
});




